package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jinpei
 * @version v1.0
 * @date 2019/5/10 22:48
 * @description TODO
 **/
@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroupDTO> queryGroupByCategory(Long id) {
        // 查询规格组
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        List<SpecGroup> specGroupList = specGroupMapper.select(specGroup);
        if (CollectionUtils.isEmpty(specGroupList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);
    }

    public List<SpecParamDTO> querySpecParams(Long gid, Long cid, Boolean searching) {
        // gid和cid必选一个
        if (gid == null && cid == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        // 查询条件
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        List<SpecParam> specParamList = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(specParamList)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
    }

    public List<SpecGroupDTO> querySpecsByCid(Long cid) {
        // 查询规格组
        List<SpecGroupDTO> specGroupDTOList = queryGroupByCategory(cid);

        // 查询规格参数，当前分类下的所有组
        List<SpecParamDTO> params = querySpecParams(null, cid, null);
        // 把规格参数按照groupId分类，放入map中，key是groupId，value是与group一致的params集合
        Map<Long, List<SpecParamDTO>> map = params.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        /*Map<Long, List<SpecParamDTO>> map = new HashMap<>();
        for (SpecParamDTO param : params) {
            Long groupId = param.getGroupId();
            if (!map.containsKey(groupId)) {
                map.put(groupId, new ArrayList<>());
            }
            map.get(groupId).add(param);
        }*/
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            specGroupDTO.setParams(map.get(specGroupDTO.getId()));
        }
        return specGroupDTOList;
    }
}
